<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增食谱类别')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-category-add">
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">类别名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="categoryName" id="categoryName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类别图片：</label>
				<div class="col-sm-8">
					<input class="form-control" type="hidden" name="categoryPic" id="pic">
					<a id="url"><img style="width: 90px;height: 90px"></a>
					<input type="file" id="categoryPic">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示顺序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="categorySort" id="categorySort" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类别状态：</label>
				<div class="col-sm-8">
				    <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">小程序可见：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="visible" th:value="${dict.dictValue}" th:checked="${dict.default}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<textarea id="remark" name="remark" class="form-control"></textarea>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "recipe/category";
	    
		$("#form-category-add").validate({
			onkeyup: false,
			rules:{
				categorySort:{
					digits:true
				},
				categoryName:{
					remote: {
						url: ctx + "recipe/category/checkCategoryNameUnique",
						type: "post",
						dataType: "json",
						data: {
							"categoryName" : function() {
								return $.common.trim($("#categoryName").val());
							}
						}
					}
				},
			},
			messages: {
		        "categoryName": {
		            remote: "类别名称已经存在"
		        }
		    },
		    focusCleanup: true
		});

		// 上传文件
		$("#categoryPic").change(function () {
			var data = new FormData();
			data.append("file", $("#categoryPic")[0].files[0]);
			$.ajax({
				type: "POST",
				url: ctx + "common/upload",
				data: data,
				cache: false,
				contentType: false,
				processData: false,
				dataType: 'json',
				success: function(result) {
					// alert(JSON.stringify(result))
					if (result.code == web_status.SUCCESS) {
						$("#url img").attr("src",result.fileName)
						$("#pic").val(result.fileName)
					}
				},
				error: function(error) {
					alert("图片上传失败。");
				}
			});
		})

		function submitHandler() {
	        if ($.validate.form()) {
	        	$.operate.save(prefix + "/add", $('#form-category-add').serialize());
	        }
	    }
	</script>
</body>
</html>
